package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.LogInfo;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.modelVo.DocumentVo;
import com.beamfield.service.manage.sys.DocumentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author Zephyr
 * @Description: 移动端--技术文档
 * @Date 2018/10/11
 */
@Controller
@RequestMapping(value = "/mb_api/document")
public class MbDocumentController extends MbBaseController {

    private final DocumentService documentService;

    @Autowired
    public MbDocumentController(DocumentService documentService) {
        this.documentService = documentService;
    }

    /**
     * 获取技术文档
     * @param request
     * @return
     */
    @RequestMapping(value = "/getDocument", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getDocument(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String query = body.getString("query");
        String id = body.getString("id");
        String offset = body.getString("offset");
        String limit = body.getString("limit");
        if(offset == null || limit == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        LogInfo logInfo = super.getLogInfo(request);
        try {
            PageResult pageResult = null;

            if(StringUtils.isEmpty(query)){

                pageResult = documentService.getDocumentNoType(offset, limit, logInfo.getBeamId(), id);
            }else{

                pageResult = documentService.getDocumentFromIndex(offset, limit, null, query, logInfo.getBeamId());
            }

            return new ApiResult(pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }

    }
}
